package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 单表分页查询
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
        List<Goods> goodsList = goodsService.getPage(page, rows, goodsName, goodsTypeId);
        HashMap<String, Object> result = new HashMap<>();
        result.put("total",goodsService.getAll());
        result.put("rows",goodsList);
        return result;
    }

    /**
     * 多表分页查询
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @PostMapping("listInventory")
    public Map<String,Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){
        List<Goods> goods = goodsService.getComplexPage(page,rows,codeOrName,goodsTypeId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",goodsService.getAll());
        map.put("rows",goods);
        return map;
    }


    /**
     * 修改或插入
     * @param goods
     * @return
     */

    @PostMapping("save")
    public ServiceVO save(Goods goods){
        goodsService.save(goods);
        return new ServiceVO(100,"请求成功");
    }

    /**
     * 商品删除（要判断商品状态,入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @PostMapping("delete")
    public ServiceVO delete(Integer goodsId){
        goodsService.delete(goodsId);
        return new ServiceVO(100,"请求成功");
    }


    /**
     * 无库存商品列表展示（可以根据商品名称或编码查询）
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        List<Goods> res = goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",res.size());
        map.put("rows",res);
        return map;
    }

    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     * @return
     */
    @PostMapping("getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        List<Goods> res = goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",res.size());
        map.put("rows",res);
        return map;
    }

    /**
     * 添加库存、修改数量或成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     * @return
     */

    @PostMapping("saveStock")
    public ServiceVO saveStock(@RequestParam Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
        goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        return new ServiceVO(100,"请求成功");
    }


    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @PostMapping("deleteStock")
    public ServiceVO deleteStock(Integer goodsId){
       return goodsService.deleteStock(goodsId);
    }


    /**
     * 库存报警（当前库存量 小于 库存下限的商品信息）
     * @return
     */
    @PostMapping("listAlarm")
    public Map<String,Object> listAlarm(){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("rows",goodsService.listAlarm());
        return map;
    }
}

